package com.lxr0860.lxr0860production_test.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by 11 on 2018/3/22.
 */

public class FileUtils {
    public static boolean writeFile(String path, String message){
        File file=new File(path);
        if(!file.exists()||!file.isFile()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter writer=null;
        try {
            writer=new FileWriter(file,true);
            writer.write(message);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取剩余可用内存
     *
     * @return
     */
    public static long getMemonry() {
        StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
        long size = statFs.getAvailableBytes();
        return size;
    }

    //创建文件夹和文件
    public static boolean creatFile(String dirPath, String fileName) {
        File dir =new File(dirPath);
        if(!dir.exists()){
            return dir.mkdir();
        }
        File file=new File(dir.getAbsolutePath()+fileName);
        if (!file.exists()) {
            try {
                return file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    //往文件中写入数据
    public static boolean writeDataToTfCard(String path) {
        File file = new File(path);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(new byte[1024]);
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
        return true;

    }

    //复制文件
    public static boolean copyFile(String path, String newpath) {
        File newFile = new File(newpath);
        if (!newFile.exists()) {
            try {
                newFile.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }
        File oldFile = new File(path);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(oldFile);
            fos = new FileOutputStream(newFile);
            byte[] data = new byte[1024];
            int len = 0;
            while ((len = fis.read(data)) != -1) {
                fos.write(data, 0, len);
            }
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;

    }


    public static boolean deleteFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return true;
        }
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (listFiles != null & listFiles.length > 0) {
                for (File f : listFiles) {
                    f.isFile();
                    if (f.delete()) {
                        continue;
                    } else {
                        return false;
                    }
                }
            }
        }
        return file.delete();
    }


    //获取外部存储的大小和剩余空间
    public static String getGB(String path) {
        StatFs sf = new StatFs(path);
        long blockSize = sf.getBlockSizeLong();
        long blockCount = sf.getBlockCountLong();
        long availCount = sf.getAvailableBlocksLong();
        return "总大小:" + (blockSize * blockCount )/ (1024 * 1024)
                + "MB\n可用大小:" + (availCount * blockSize) / (1024 * 1024) + "MB";
    }
}
